package com.rectcircle.wechat.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * 本类封装了相关安全的算法基础
 * @author RectCircle
 * @version 0.2
 * @time 2016年7月11日-下午5:18:13
 * Copyright (c) 2016 RectCircle-版权所有
 *
 */
public class SecurityAlgorithm {
	
	/**
	 * 对字符串进行sha1摘要
	 * @param src
	 * @return sha1摘要
	 */
	public static String encryptBySHA1(String src) {
  		byte[] bytes = null;
  		StringBuffer result = new StringBuffer();

  		try {
  			MessageDigest md = MessageDigest.getInstance("SHA");
  			md.update(src.getBytes("utf-8"));
  			bytes = md.digest();

  			for (int i = 0; i < bytes.length; i++) {
  				result.append(Integer.toHexString(
  				(0x000000ff & bytes[i]) | 0xffffff00).substring(6));
  			}
  		} catch (Exception ex) {
  			return null;
  		}

  		return result.toString();
  	}

	/**
	 * 
	 * 对字符串进行32位MD5摘要
	 * 
	 * @param src
	 * @return
	 */
	public static String encryptByMD5(String src) { 
        String re_md5 = new String();
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(src.getBytes());
            byte b[] = md.digest();
 
            int i;
 
            StringBuffer buf = new StringBuffer("");
            for (int offset = 0; offset < b.length; offset++) {
                i = b[offset];
                if (i < 0)
                    i += 256;
                if (i < 16)
                    buf.append("0");
                buf.append(Integer.toHexString(i));
            }
 
            re_md5 = buf.toString();
 
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return re_md5;
    }
	

}
